package com.bargate.domain.spring.expression.value;

import com.bargate.domain.spring.expression.Storm;
import com.bargate.domain.spring.expression.util.DateParser;
import com.google.common.collect.Lists;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StopWatch;

import java.util.List;
import java.util.Queue;

/**
 * Created by IntelliJ IDEA.
 * User: EAD-MASTER
 * Date: 21/09/2010
 * Time: 23:52:31
 * To change this template use File | Settings | File Templates.
 */
public class CollectionSelectionOperations implements BeanFactoryAware {

    @Value("#{Storms.?[occurred != null and occurred.getTime()>=T(com.bargate.domain.spring.expression.util.DateParser).parse('01/01/2000').getTime()]}")
    private List<Storm> occurredAfterYear2000;
    private List<Storm> occurredAfterYear2000NoSpEL;
    private long timeItTook;
    @Value("#{Storms.?[Type.startsWith('T')]}")
    private List<Storm> startWithT;
    @Value("#{Storms.?[Type == 'Tornado']}")
    private List<Storm> strongestWind;
    private BeanFactory factory;

    public List<Storm> getStartWithT() {
        return startWithT;
    }

    public List<Storm> getOccurredAfterYear2000() {
        return occurredAfterYear2000;
    }

    public long getTimeItTook() {
        return timeItTook;
    }

    public List<Storm> getOccurredAfterYear2000NoSpEL() {
        long startTime = System.nanoTime();
        StopWatch watch = new StopWatch();
        watch.start();
        occurredAfterYear2000NoSpEL = (List<Storm>) factory.getBean("Storms");
        List<Storm> storms = Lists.newArrayList();
        for (Storm storm : occurredAfterYear2000NoSpEL) {
            if (storm.getOccurred() != null && storm.getOccurred().getTime() >= DateParser.parse("01/01/2000").getTime()) {
                storms.add(storm);
            }
        }
        long endTime = System.nanoTime();
        watch.stop();
        timeItTook =( endTime - startTime );
        return storms;
    }

    @Value("#{OrderedStorms}")
    private Queue<Storm> abcOrderedStorms;

    public Queue<Storm> getAbcOrderedStorms() {
        return abcOrderedStorms;
    }

    @Value("#{Storms.^[maxWindVelocity > 100]}")
    private List<Storm> firstStrongestWind;

    public String getFirstStrongestWind() {
        return firstStrongestWind.get(0).getType();
    }

    @Value("#{Storms.$[Distance == null]}")
    private List<Storm> lastNullDistanceStorm;

    public String getStrongestWind() {
        return strongestWind.get(0).getType();
    }

    public Storm getLastNullStormDistance() {
        return lastNullDistanceStorm.get(0);
    }


    @Value("#{Storms.?[#this.distance!=null and #this.distance>100]}")
    private List<Storm> selectedStorms;

    public List<Storm> getSelectedStorms() {
        return selectedStorms;
    }

    @Value("#{NumbersList.?[#this %2 == 0]}")
    private List<Integer> evenNumbersList;

    public List<Integer> getEvenNumbersList() {
        return evenNumbersList;
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        factory = beanFactory;
    }
}
